package com.yichuang.wuziqi.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.yichuang.wuziqi.R;
import com.yichuang.wuziqi.client.Client;
import com.yichuang.wuziqi.domain.Data;
import com.yichuang.wuziqi.game.LocalGame;
import com.yichuang.wuziqi.player.AI;
import com.yichuang.wuziqi.player.LocalAI;
import com.yichuang.wuziqi.player.LocalPlayer;
import com.yichuang.wuziqi.view.BoardPanel;

public class NetworkGameActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "xcm";

    private Button readyBtn;
    private Button endBtn;
    private Button takeBackBtn;
    private BoardPanel boardPanel;
    private TextView myTimeTv;
    private TextView oppositeTimeTv;

    private Spinner localPlayerSpinner;
    private Spinner networkPlayerSpinner;

    private Client client;
    private int currentTurn = -1;
    private int myTime;
    private int oppositeTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.network_game);
        initView();
    }

    private void initGame() {
        client.setRecorder(boardPanel);
        client.setGameListener(new Client.GameListener() {
            @Override
            public void onGameStatusChange(int status) {
                switch (status) {
                    case Data.ServerAction.GAME_OVER:
                        readyBtn.setEnabled(true);
                        endBtn.setEnabled(false);
                        break;
                    case Data.ServerAction.GAME_START:
                        endBtn.setEnabled(true);
                        break;
                }
            }

            @Override
            public void onTurn(int side) {
                if (side != currentTurn) {
                    //把之前的逻辑
                    currentTurn = side;
                }
            }
        });
    }

    private void initView() {
        readyBtn = findViewById(R.id.ready);
        endBtn = findViewById(R.id.admit_beaten);
        takeBackBtn = findViewById(R.id.take_back);
        myTimeTv = findViewById(R.id.myTime);
        oppositeTimeTv = findViewById(R.id.oppositeTime);

        readyBtn.setOnClickListener(this);
        endBtn.setOnClickListener(this);
        endBtn.setEnabled(false);
        takeBackBtn.setOnClickListener(this);

        boardPanel = findViewById(R.id.panel);
        localPlayerSpinner = findViewById(R.id.spinner_white);
        networkPlayerSpinner = findViewById(R.id.spinner_black);

        localPlayerSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        client = new Client(NetworkGameActivity.this,
                                new LocalPlayer(boardPanel));
                        break;
                    case 1:
                        client = new Client(NetworkGameActivity.this,
                                new LocalAI(LocalGame.MAX_LINE, AI.LEVEL_EASY));
                        break;
                    case 2:
                        client = new Client(NetworkGameActivity.this,
                                new LocalAI(LocalGame.MAX_LINE, AI.LEVEL_MIDDLE));
                        break;
                    case 3:
                        client = new Client(NetworkGameActivity.this,
                                new LocalAI(LocalGame.MAX_LINE, AI.LEVEL_HARD));
                        break;
                }
                initGame();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        networkPlayerSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0:
                        if (client.isReady()) {
                            client.sendToServer(new Data(Data.GameConfig.GAME_MAN, ""));
                        }
                        break;
                    case 1:
                        if (client.isReady()) {
                            client.sendToServer(new Data(Data.GameConfig.GAME_AI, ""));
                        }
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }

        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ready:
                boardPanel.reset();
                if (!client.readyForGame()) {
                    Toast.makeText(NetworkGameActivity.this, "连接服务器失败，请重试",
                            Toast.LENGTH_SHORT).show();
                    return;
                }
                readyBtn.setEnabled(false);
                endBtn.setEnabled(false);
                break;
            case R.id.admit_beaten:
                readyBtn.setEnabled(true);
                endBtn.setEnabled(false);
                break;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        client.setGameOver(false);
        super.onDestroy();
    }
}
